package com.glory.brepda.activity.mfg;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.XXChangeShiftsAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.entity.ChangeShiftEntity;
import com.glory.brepda.entity.LotInfoEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.LotUtils;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Set;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

public class BGShiftExchangeActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.tvWorkNo)
    TextView tvWorkNo;
    @BindView(R.id.etShift)
    FreeUI_ClearEditText etShift;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.list_tv1)
    TextView listTv1;
    @BindView(R.id.list_tv2)
    TextView listTv2;
    @BindView(R.id.list_tv3)
    TextView listTv3;
    @BindView(R.id.list_tv4)
    TextView listTv4;
    @BindView(R.id.list_tv5)
    TextView listTv5;
    @BindView(R.id.list_tv6)
    TextView listTv6;
    @BindView(R.id.list_tv7)
    TextView listTv7;
    @BindView(R.id.rvLotInfoList)
    RecyclerView rvLotInfoList;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvButton)
    TextView tvButton;
    @BindView(R.id.tvComment)
    TextView tvComment;
    @BindView(R.id.ivComment)
    ImageView ivComment;
    @BindView(R.id.flComment)
    FrameLayout flComment;
    private XXChangeShiftsAdapter mChangeShiftsAdapter;
    private LotInfoEntity lotInfoEntity;
    private ArrayList<String> mCommentList = new ArrayList<>();
    private FreeUI_GeneralPop mCommentPop;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_shift_exchange;
    }

    @Override
    protected void initView() {
        initLayout();
        mChangeShiftsAdapter = new XXChangeShiftsAdapter(null);
        mChangeShiftsAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mChangeShiftsAdapter.isFirstOnly(true);
        rvLotInfoList.setAdapter(mChangeShiftsAdapter);

        mCommentList.add("包抽一体");
        mCommentList.add("只包不抽");
        mCommentList.add("只抽不包");
        mCommentList.add("自动包装机");

        initEdit(etLot, s -> LotUtils.searchLotInfo(BGShiftExchangeActivity.this, s, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
            @Override
            public void onSuccess(LotInfoEntity response) {
                lotInfoEntity = response;
                if (!response.getLOT().getSTATE().equals("正在作业")){
                    showToast("只有正在作业可以交班",false);
                    return;
                }
//                if (getText(etShift).equals("")){
//                    showToast("请输入工号",false);
//                    return;
//                }
//                for (ChangeShiftEntity entity : mChangeShiftsAdapter.getData()){
//                    if (entity.getITEMID().equals(response.getLOT().getLOTID())){
//                        showToast("批次已添加到列表",false);
//                        return;
//                    }
//                }
                //重新构建实体类，将接口需要字段传入
                ChangeShiftEntity changeShiftEntity = new ChangeShiftEntity(response.getLOT().getLOTID(),
                        response.getLOT().getMAINQTY(),
                        response.getLOT().getEQUIPMENTID(),
                        response.getLOT().getSTATE(),
                        getText(etShift),
                        "BG");
                changeShiftEntity.setRESERVED05(response.getLOT().getSTEPNAME());
                changeShiftEntity.setRESERVED06(response.getLOT().getSTEPDESC());
                mChangeShiftsAdapter.addData(changeShiftEntity);
                etLot.setText("");
            }

            @Override
            public void onFail(Throwable t) {
                loadError(t,"LOTQUERY");
            }
        }));
    }

    private void initLayout() {
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        setMainLayout(llMain);
        listTv1.setText("批号");
        listTv2.setText("批次数量");
        listTv3.setText("设备编号");
        listTv4.setText("批次状态");
        listTv5.setText("已完成数量");
        listTv6.setText("交班人");
        listTv7.setText("清除");
        listTv5.setVisibility(View.VISIBLE);
        listTv6.setVisibility(View.VISIBLE);
        listTv7.setVisibility(View.VISIBLE);
        tvButton.setVisibility(View.VISIBLE);
        initRecyclerView(rvLotInfoList,this);
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.tvBaseSubmit,R.id.tvButton,R.id.flComment})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.tvButton:
                HashMap<String, Integer> map = new HashMap<>();
                for (ChangeShiftEntity data:mChangeShiftsAdapter.getData()) {
                    map.put(data.getITEMID(),Integer.parseInt(data.getMAINQTY()));
                }
                Set<String> strings = map.keySet();
                for (String ms:strings) {
                    ArrayList<ChangeShiftEntity> changeShiftEntities = new ArrayList<>();

                    for (int i = 0; i < mChangeShiftsAdapter.getData().size(); i++) {
                        if (mChangeShiftsAdapter.getData().get(i).getITEMID().equals(ms)){
                            changeShiftEntities.add(mChangeShiftsAdapter.getData().get(i));
                        }
                    }
                    for (ChangeShiftEntity data:mChangeShiftsAdapter.getData()){
                        if (data.getITEMID().equals(ms)){
                            int n = map.get(ms) / changeShiftEntities.size();
                            data.setXXMAINQTY(String.valueOf(n));
                            data.setCOMMENTS(String.valueOf(n));
                        }
                    }
                }
                mChangeShiftsAdapter.notifyDataSetChanged();
                break;
            case R.id.tvBaseSubmit:
//                if(getText(etShift).equals("")){
//                    showToast("请输入交班人",false);
//                    return;
//                }

                if (mChangeShiftsAdapter.getData().size() <= 0){
                    showToast("请添加列表信息后操作",false);
                    return;
                }
                if (tvComment.getText().toString().equals("")){
                    showToast("请选择备注列表",false);
                    return;
                }

                showSubmitDialog(new onSubmitListener() {
                    @Override
                    public void setOnSubmitListener() {
                        FreeUI_EntityCallBack<BaseResponseBean<ChangeShiftEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<ChangeShiftEntity>>
                                (new TypeToken<BaseResponseBean<ChangeShiftEntity>>() {
                                }.getType(), getSupportFragmentManager(), this) {

                            @Override
                            public void onSuccess(Response<BaseResponseBean<ChangeShiftEntity>> response) {
                                super.onSuccess(response);
                                if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())){
                                    showConfirmDialog(BGShiftExchangeActivity.this,"交班成功，单据号为:"
                                            + response.body().getResponse().getBody().getDOCID());
                                }
                            }

                            @Override
                            public void onError(Response<BaseResponseBean<ChangeShiftEntity>> response) {
                                super.onError(response);
                                loadError(response.getException(),"CHANGESHIFT");
                            }
                        };
                        Date date = new Date();
                        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        String sim = dateFormat.format(date);

                        JsonObject map = new JsonObject();
                        map.addProperty("ACTIONTYPE","SPECIALREPORT");
                        map.addProperty("reserved8","BG");
                        map.addProperty("REFEROWNER",getText(etShift));
                        map.addProperty("COMMENTS",tvComment.getText().toString());
                        map.add("CHANGESHIFTLINELIST",new JsonParser().parse(new Gson().toJson(mChangeShiftsAdapter.getData())).getAsJsonArray());

                        NetUtils.requestPostNet("IF.CHANGESHIFTREPORTE","Common",BGShiftExchangeActivity.this,map,callBack);
                    }
                });
                break;
            case R.id.flComment:
                if (mCommentPop != null) {
                    if (mCommentPop.isShowing()) {
                        mCommentPop.dismiss();
                    }
                } else {
                    mCommentPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flComment.getWidth(), mCommentList, ivComment, (codeS, i) -> {
                        tvComment.setText(codeS);
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivComment);
                mCommentPop.showAsDropDown(flComment, 0, 0);
                break;
        }

    }


}
